<template>
  <div>
    <Top :title="headTitle" iconShow="1"></Top>
    <div class="main">
      <div class="msg">请绑定持卡人本人{{title}}</div>
      <div class="top">
        <div class="person">
          <span>持卡人</span>
          <input type="text" placeholder="请输入您的真实姓名" @keyup="nameKeyup" v-model="realName" :disabled="canwrite1">
          <div class="img" @click="nameMsg"><img src="../../../assets/enchash/tishi.png" alt=""></div>
        </div>
        <div class="personCard">
          <span>身份证</span>
          <input type="tel" placeholder="请输入身份证号" @keyup="idcardKeyup" v-model="idcardCode" :disabled="canwrite2">
        </div>
      </div>
      <div class="bottom">
        <div class="bankNO">
          <span>卡号</span>
          <input type="tel" placeholder="请输入卡号" v-model="bankNO" @keyup="bankCardKeyup">
        </div>
        <div class="bankName">
          <span>银行</span>
          <span class="checkBank">
						<select class="" v-model="bankCode">
							<option value="">请选择开户银行</option>
              <option :value="item.bankCode" v-show="active==2" v-for="item in bankList" >{{item.bankName}}</option>
              <option :value="item.bankCode" v-show="active==1" v-for="item in creditCardList">{{item.bankName}}</option>
						</select>
					</span>
        </div>
        <div class="" v-show="active==1">
          <span>有效期</span>
          <input type="number" placeholder="月份年份，如1017" v-model="validDate">
          <div class="img" @click="youxiaoqiMsg"><img src="../../../assets/enchash/tishi.png" alt=""></div>
        </div>
        <div class="" v-show="active==1">
          <span>安全码</span>
          <input type="text" placeholder="卡片背面签名栏后三位" v-model="validNo">
          <div class="img" @click="anquanmaMsg"><img src="../../../assets/enchash/tishi.png" alt=""></div>
        </div>
        <div class="phone">
          <span>手机号</span>
          <input type="tel" placeholder="请输入手机号" v-model="formdata.tel"  @keyup="phoneKeyup">
        </div>
        <div class="code">
          <span>验证码</span>
          <input type="text" placeholder="请输入验证码" v-model="smsCode">
          <!-- <p class="getCode" @click="getCode">获取验证码</p> -->
          <div style="oveflow:hidden;" @click="getImgCode" v-bind:class="{'getcodeA': flag, 'getcodeB': !flag}">{{codeTime}}</div>
        </div>
      </div>
      <div class="btn" @click="sublimt">确认添加</div>
    </div>


    <!-- 验证码弹框 -->
    <div class="verifycode-model" v-if="verifyShow">
      <div class="verifycode-model-inner">
        <div class="verifycode-model-inner-top">请输入图形验证码
          <i class="iconfont icon-guanbi" @click="closeModel"></i>
        </div>
        <div class="verifycode-model-inner-content">
          <input type="text" v-model="formdata.verifyCode" />
          <img :src="codePic" @click="verifyCode" />
        </div>
        <div class="verifycode-model-inner-footer" @click="startTime">确定</div>
      </div>
    </div>

    <!-- 有效期弹框 -->
    <div class="youxiaoqi" v-if="youxiaoqiShow">
      <div class="inner">
        <div class="title">有效期</div>
        <div class="msg">有效期是银行卡正面卡号下方的四位数，输入格式为“月份年份”，如1017</div>
        <img src="../../../assets/enchash/youxiaoqi.png" alt="">
        <div class="footer" @click="youxiaoqiHide">知道了</div>
      </div>
    </div> 
    <!-- 安全码弹框 -->
    <div class="youxiaoqi" v-if="anquanmaShow">
      <div class="inner">
        <div class="title">安全码</div>
        <div class="msg">银行卡背面签名栏后三位数，如“621”</div>
        <img src="../../../assets/enchash/anquanma.png" alt="">
        <div class="footer" @click="anquanmaHide">知道了</div>
      </div>
    </div>


  </div>
</template>

<script>
import Vue from 'vue'
import '../../../js/lib/flexble.js'
import util from '../../../js/lib/util.js'
import commons from '../../../common/js/common.js'
import Top from 'components/Header/Header'
import { Indicator, MessageBox } from "mint-ui";
export default {
  name: 'Addcard',
  components: { Top },
  data() {
    return {
      headTitle: '',
      title:'',
      idcardCode:'',
      realName:'',
      bankNO:'',
      bankCode:'',
      active:'',
      bankList:[],
      creditCardList:[],
      formdata: {
        tel: '',//手机号
        verifyCode: ''//用户输入的验证码(图形验证码)
      },
      smsCode:'',
      codeTime: '获取验证码',
      flag: true,
      iconShow: true,
      codePic: "/loansupermarket-app/img/getVerifyCode?num="+Math.random(),
      verifyShow: false,
      canwrite1:false,//姓名是否可以输入
      canwrite2:false,//身份证时候可以输入
      youxiaoqiShow:false,
      anquanmaShow:false,
      validDate:'',//有效期
      validNo:''//安全码
    }
  },
  mounted(){
    Vue.http.interceptors.push((request, next) => { //请求拦截器
      //请求前
      request.headers.set('token', commons.getCookie('token')); //设置请求头
      Indicator.open('加载中...'); //请求loading
      next((response) => {
        //请求后
        Indicator.close(); //隐藏loading
        return response
      })
    })

    this.active = util.getQueryString('active');//common.getParameter('active');
    this.title = this.active=='1'?'信用卡':'储蓄卡';
    this.headTitle = '添加'+this.title;
    
    //获取支持的储蓄卡银行列表
    this.bankList = JSON.parse(window.localStorage.getItem('bankList'));

    //获取支持的信用卡银行列表
    this.creditCardList = JSON.parse(window.localStorage.getItem('creditCardList'));

    //查询用户信息
    this.idcardCode = sessionStorage.getItem('idcardCode')||'';
    this.realName = sessionStorage.getItem('realName')||'';
    if(sessionStorage.getItem('realName')){
      this.canwrite1=true;
    }
    if(sessionStorage.getItem('idcardCode')){
      this.canwrite2=true;
    }
    
    MessageBox.alert('为了保障账户资金安全，只能绑定认证用户本人的'+this.title+'，否则不能取现', '持卡人说明');
  },
  methods: {
    nameKeyup(){
      this.realName=this.realName.replace(/[^\u4E00-\u9FA5]/g,'');
    },
    idcardKeyup(){
      this.idcardCode=this.idcardCode.replace(/[^\w\.\/]/ig,'');
    },
    bankCardKeyup(){
      this.bankNO=this.bankNO.replace(/\.\d*|[^\d]+/,'');
    },
    phoneKeyup(){
      this.formdata.tel=this.formdata.tel.replace(/\.\d*|[^\d]+/,'');
    },
    youxiaoqiMsg(){
      this.youxiaoqiShow=true;
    },
    anquanmaMsg(){
      this.anquanmaShow=true;
    },
    youxiaoqiHide(){
      this.youxiaoqiShow=false;
    },
    anquanmaHide(){
      this.anquanmaShow=false;
    },
    // 获取图片验证码
    getImgCode() {
      this.formdata.verifyCode = '';
      if (this.formdata.tel == "") {
        MessageBox.alert('手机号码不能为空！', '提示');
        return;
      }
      if (!(/^1[34578]\d{9}$/.test(this.formdata.tel))) {
        MessageBox.alert('手机号码输入有误！', '提示');
        return;
      }
      if(this.active=='1'){//信用卡（需要验证银行编码，卡号）
        if(!this.bankCode){
          MessageBox.alert('请选择开户银行', '提示');
          return;
        }
        if(!this.bankNO){
          MessageBox.alert('请输入银行卡号', '提示');
          return;
        }
      }
      // 重新获取验证码
      this.verifyCode();
      // 关闭模态框
      if (this.flag) {
        this.verifyShow = true;
      }
    },
    //获取验证码判断
    getCode(){
      if (!(/^1[34578]\d{9}$/.test(this.formdata.tel))) {   //正则判断手机号码是否正确
        MessageBox('提示', '手机号码输入有误！');
        return
      }
      // 验证码60s
      this.flag = !this.flag;
      let time = 60;
      let that = this;
      if (!this.flag) {
        var timer = setInterval(function () {
          time -= 1;
          that.codeTime = time + 's后可重新获取';
          if (time == 0) {
            clearInterval(timer);
            that.codeTime = '获取验证码'
            that.flag = true;
          }
        }, 1000)
      };
      // 短信验证码post参数
      let postData = {
        phone: this.formdata.tel,
        smsType: this.active=='1'?'4':'3',//3储蓄卡  4信用卡
        imgCode: this.formdata.verifyCode
      }
      
        if(this.active=='1'){
          let sendSmsDate = {
            phoneCode:this.formdata.tel ,
            bankCode:this.bankCode,
            cardType: '2',
            cardCode: this.bankNO
          }
          // 信用卡获取短信验证码
          this.$http.post('/loansupermarket-pay/card/bind/sendSmsCode',sendSmsDate)
            .then((response) => {
              var resBody = response.body;
              if (resBody.code == '000') {
                
              }else if(resBody.code == '444'){
                window.location.href='../user/login.html?history=1';
              }else {
                this.flag = !this.flag;
                MessageBox.alert(resBody.mesg, '提示');
                clearInterval(timer);
                that.codeTime = '获取验证码'
              }
            })
        }else if(this.active=='2'){
          // 储蓄卡获取短信验证码
          this.$http.get('/loansupermarket-app/sms/sendSmsByImg?reqType=1&phone='+postData.phone+'&smsType='+postData.smsType+"&imgCode="+postData.imgCode)
            .then((response) => {
              var resBody = response.body;
              if (resBody.code == '000') {
                
              }else if(resBody.code == '444'){
                window.location.href='../user/login.html?history=1';
              }else {
                this.flag = !this.flag;
                MessageBox.alert(resBody.mesg, '提示');
                clearInterval(timer);
                that.codeTime = '获取验证码'
              }
            })
        }
    },
    //调用图形验证码
    verifyCode() {
      this.codePic = "/loansupermarket-app/img/getVerifyCode?num="+Math.random();
    },
    // 关闭图形验证码模态框
    closeModel() {
      this.verifyShow = false;
    },
    // 启动获取验证码
    startTime(flag) {
      this.verifyShow = false;
      if(this.formdata.verifyCode.length==0){
        MessageBox.alert('请输入图形验证码', '提示');
        return;
      }
      if (flag) {
        this.getCode();
      }
    },
    // 提交
    sublimt(){
      let prePath = util.getQueryString('prePath');
      if(this.realName<2 ||this.realName>15){
        MessageBox.alert('姓名长度不正确');
        return;
      }else if(!/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/.test(this.idcardCode)){
        MessageBox.alert('请输入正确的身份证号码');
        return;
      }else if(!this.bankNO){
        MessageBox.alert('请输入卡号');
        return;
      }else if(!this.bankCode){
        MessageBox.alert('请选择银行');
        return;
      }if (!(/^1[34578]\d{9}$/.test(this.formdata.tel))) {   //正则判断手机号码是否正确
        MessageBox.alert('手机号码输入有误！');
        return
      }else if(!this.smsCode){
        MessageBox.alert('请输入验证码');
        return;
      }
      
      let sublimtData={
        realName:this.realName,
        idcardCode:this.idcardCode,
        cardCode:this.bankNO,
        bankCode:this.bankCode,
        phoneCode:this.formdata.tel,
        smsCode:this.smsCode,
        cardType:1,
        channel:window.sessionStorage.getItem('channel')?window.sessionStorage.getItem('channel'):''
      }
      let sublimtData1={
        realName:this.realName,
        idcardCode:this.idcardCode,
        cardCode:this.bankNO,
        bankCode:this.bankCode,
        phoneCode:this.formdata.tel,
        smsCode:this.smsCode,
        cardType:2,
        channel:window.sessionStorage.getItem('channel')?window.sessionStorage.getItem('channel'):'',
        validDate:this.validDate,//有效期
        validNo:this.validNo//安全码
      }

      if(this.active=='1'){//地址栏active=1，信用卡
        if(!/^(\d{16})$/.test(this.bankNO)){
          MessageBox.alert('请输入正确的卡号');
          return;
        }

        this.$http.post('/loansupermarket-pay/card/bind/checkBankCard',{cardType:2,cardCode:sublimtData1.cardCode,bankCode:sublimtData1.bankCode})
        .then((response) => {
          var resBody = response.body;
          if (resBody.code == '000') {//卡号和银行名字匹配成功
            //信用卡----提交请求
            this.$http.post('/loansupermarket-pay/card/bind/bindCreditCard',sublimtData1)
              .then((response) => {
                var resBody = response.body;
                if (resBody.code == '000') {
                  window.localStorage.setItem('active',1);
                  history.go(-1);
                }else if(resBody.code == '444'){
                    location.href='../user/login.html?history=1';
                }else{
                  if(resBody.mesg){
                    MessageBox.alert(resBody.mesg, '提示');
                  }
                }
            })
          }else if(resBody.code == '444'){
            window.location.href='../user/login.html?history=1';
          }else if(resBody.code == '111'){//卡号和银行名字不匹配
          
            MessageBox.confirm(resBody.mesg,'提示',{
              cancelButtonText: '修改',
              confirmButtonText:'继续'
            }).then(()=>{
              //信用卡----提交请求
              this.$http.post('/loansupermarket-pay/card/bind/bindCreditCard',sublimtData1)
                .then((response) => {
                  var resBody = response.body;
                  if (resBody.code == '000') {
                    window.localStorage.setItem('active',1);
                    // this.$router.go(-1);
                    window.history.go(-1);
                  }else if(resBody.code == '444'){
                      window.location.href='../user/login.html?history=1';
                  }else{
                    if(resBody.mesg){
                      MessageBox.alert(resBody.mesg, '提示');
                    }
                  }
              })
            }).catch(
              ()=>{}
            )

              
            
          }else{
            if(resBody.mesg){
              MessageBox.alert(resBody.mesg, '提示');
            }
          }
        })

        
      }else if(this.active=='2'){//地址栏active=2，储蓄卡
        if(!/^(\d{16,19})$/.test(this.bankNO)){
          MessageBox.alert('请输入正确的卡号');
          return;
        }

        this.$http.post('/loansupermarket-pay/card/bind/checkBankCard',{cardType:1,cardCode:sublimtData.cardCode,bankCode:sublimtData.bankCode})
        .then((response) => {
          var resBody = response.body;
          if (resBody.code == '000') {//卡号和银行名字匹配成功
            //储蓄卡----提交请求
            this.$http.post('/loansupermarket-pay/card/bind/bindDebitCard',sublimtData)
              .then((response) => {
                var resBody = response.body;
                if (resBody.code == '000') {
                  window.sessionStorage.setItem('idcardCode',this.idcardCode);
                  window.sessionStorage.setItem('realName',this.realName);
                  window.localStorage.setItem('active',2);
                  if(prePath == "completeInfo"){
                    location.href = "../../user/completeInfo.html?isMoneyCard=true&isIdCard=true";
                  }else{
                    window.history.go(-1);
                  }
                }else if(resBody.code == '444'){
                  window.location.href='../user/login.html?history=1';
                }else {
                  if(resBody.mesg){
                    MessageBox.alert(resBody.mesg, '提示');
                  }
                }
            })
            
          }else if(resBody.code == '444'){
            window.location.href='../user/login.html?history=1';
          }else if(resBody.code == '111'){//卡号和银行名字不匹配

            MessageBox.confirm(resBody.mesg,'提示',{
              cancelButtonText: '修改',
              confirmButtonText:'继续'
            }).then(()=>{
              //储蓄卡----提交请求
              this.$http.post('/loansupermarket-pay/card/bind/bindDebitCard',sublimtData)
                .then((response) => {
                  var resBody = response.body;
                  if (resBody.code == '000') {
                    window.sessionStorage.setItem('idcardCode',this.idcardCode);
                    window.sessionStorage.setItem('realName',this.realName);
                    window.localStorage.setItem('active',2);
                    if(prePath == "completeInfo"){
                      location.href = "../../user/completeInfo.html?isMoneyCard=true&isIdCard=true";
                    }else{
                      window.history.go(-1);
                    }

                  }else if(resBody.code == '444'){
                    window.location.href='../user/login.html?history=1';
                  }else {
                    if(resBody.mesg){
                      MessageBox.alert(resBody.mesg, '提示');
                    }
                  }
              })
            }).catch(
              ()=>{}
            )

          }else{
            if(resBody.mesg){
              MessageBox.alert(resBody.mesg, '提示');
            }
          }
        })

        
      }
      


    },
    //名字提示
    nameMsg(){
      MessageBox.alert('为了保障账户资金安全，只能绑定认证用户本人的储蓄卡', '持卡人说明');
    }
    
  }
}
</script>
<style lang="scss" scoped>
@import '../../../scss/base.scss';
.main{
  margin-top: 1.2rem;
  .msg{
    height: 1.3rem;
    line-height: 1.3rem;
    padding-left: 0.53rem;
    text-align: left;
    font-size: 0.42rem;
    color:#666;
  }
  .top{
    padding: 0 0.53rem;
    background:#fff;
    div{
      height: 1.3rem;
      line-height: 1.3rem;
      border-bottom: 1px solid #e5e5e5;
      text-align: left;
      overflow-y: hidden;
      span{
        float: left;
        width: 2.24rem;
        color:#666;
        font-size: 0.37rem;
      }
      input{
        float: left;
        width: 5.5rem;
        padding:0;
        margin:0;
        height: 1.3rem;
        line-height: 1.3rem;
        border:none;
        font-size: 0.37rem;
        color:#333;
        outline:0;
        background: #fff;
      }
      
      .img{
        float: right;
        width: 0.45rem;
        height: 0.4rem;
        margin-top: 0.42rem;
        position: relative;
        img{
          position: absolute;
          height: 0.4rem;
          top: 0;
          right:0;
        }
        form{
          height: 0.4rem;
          input{
            position: absolute;
            left: 0;
            z-index: 10;
            width: 100%;
            height: 100%;
            opacity: 0;
          }
        }
        
      }
    }
    div:last-child{
      border: none;
    }
  }
  .bottom{
    padding: 0 0.53rem;
    background:#fff;
    margin-top: 0.26rem;
    &>div{
      overflow: hidden;
    }
    div{
      height: 1.3rem;
      line-height: 1.3rem;
      border-bottom: 1px solid #e5e5e5;
      text-align: left;
      span{
        float: left;
        width: 2.24rem;
        color:#666;
        font-size: 0.37rem;
      }
      input{
        float: left;
        width: 5.5rem;
        padding:0;
        margin:0;
        height: 1.3rem;
        line-height: 1.3rem;
        border:none;
        font-size: 0.37rem;
        color:#333;
        outline:0;
      }
      .checkBank{
        float: left;
        width: auto;
        padding:0;
        margin:0;
        height: 1.3rem;
        line-height: 1.3rem;
        border:none;
        font-size: 0.37rem;
        color:#333;
        select{
          border: none;
          width: auto;
          height: 100%;
          appearance: none;
          outline:0;
          padding:0 2%;
          font-size: 0.37rem;
          option{
            font-size:0.37rem;
            text-align:center;
          }
        }
      }
      .img{
        float: right;
        width: 0.45rem;
        height: 0.4rem;
        margin-top: 0.42rem;
        position: relative;
        img{
          position: absolute;
          height: 0.4rem;
          top: 0;
          right:0;
        }
        form{
          height: 0.4rem;
          input{
            position: absolute;
            left: 0;
            z-index: 10;
            width: 100%;
            height: 100%;
            opacity: 0;
          }
        }
      }
      .getCode{
        float: left;
        width: 2.5rem;
        height: 0.66rem;
        line-height: 0.66rem;
        border:1px solid #00dada;
        color: #00dada;
        text-align: center;
        margin-top:0.33rem;
        border-radius: 0.1rem;
      }
    }
    .code{
      input{
        width: 4rem;
      }
    }
    div:last-child{
      border: none;
    }
  }
  .btn{
    margin:0 auto;
    margin-top: 0.8rem;
    width: 9rem;
    height: 1.2rem;
    line-height: 1.2rem;
    font-size: 0.4rem;
    color:#fff;
    background: #00dada;
    border-radius: 0.15rem;
  }
  input::-input-placeholder{
    font-size:0.37rem ;
  }
}
.verifycode-model {
  width: 100%;
  height: 100Vh;
  background: rgba(0, 0, 0, 0.3);
  position: fixed;
  left: 0;
  z-index: 11;
  text-align: left;
  top: 0;
  &-inner {
    border-radius: 0.12rem;
    overflow: hidden;
    width: 8rem;
    height: 5.3rem;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    &-top {
      width: 100%;
      height: 1.5rem;
      background: #d8fcff;
      font-size: 0.45rem;
      box-sizing: border-box;
      padding-left: 0.34rem;
      color: gary;
      padding-top: 0.25rem;
      text-align: center;
      line-height: 1rem;
      color: #000000;
      .iconfont {
        position: absolute;
        right: 0.34rem;
        top: 0.3rem;
        font-size: 0.6rem;
      }
    }
    &-content {
      box-sizing: border-box;
      padding: 0.8rem 0.4rem;
      input {
        width: 4.5rem;
        box-sizing: border-box;
        padding-left: 0.34rem;
        border: 1px solid #aaaaaa;
        outline: none;
        border-radius: 0.12rem;
        height: 1rem;
        float: left;
      }
      img {
        float: right;
        background: #d8fcff;
        width: 2.5rem;
        height: 1rem;
      }
    }
    &-footer {
      position: absolute;
      bottom: 0;
      left: 0;
      height: 1.2rem; // background: $primaryColor;
      background: -webkit-linear-gradient(right, #37c4d2, #6be3e6) !important;
      width: 100%;
      text-align: center;
      font-size: 0.45rem;
      line-height: 1.2rem;
      color: #fff;
    }
  }
}
.bottom .code .getcodeA {
  overflow: hidden;
  float: right;
  width: 2.5rem;
  height: 0.66rem;
  line-height: 0.66rem;
  background:-webkit-linear-gradient(left, #2ce9e8, #7dd7ff);
  display: inline-block;
  border-radius: 4px;
  margin-top: 0.35rem;
  text-align: center;
  font-size: 0.36rem;
  color: #fff;
  img {
    width: 100%;
    height: 100%;
  }
}

.bottom .code .getcodeB {
  overflow: hidden;
  float: right;
  width: 2.5rem;
  height: 0.66rem;
  line-height: 0.66rem;
  text-align: center;
  background: #808080;
  display: inline-block;
  border-radius: 4px;
  margin-top: 0.35rem;
  font-size: 0.36rem;
  color: #fff;
  img {
    width: 100%;
    height: 100%;
  }
}
.youxiaoqi{
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background: rgba(0,0,0,0.3);
  .inner{
    position: absolute;
    top: 50%;
    left: 50%;
    width: 6.6rem;
    height: 6.6rem;
    margin-left: -3.3rem;
    margin-top: -3.3rem;
    background:#fff ;
    border-radius: 0.15rem;
    .title{
      height: 1.6rem;
      line-height: 1.6rem;
      font-size: 0.44rem;
    }
    .msg{
      font-size: 0.32rem;
    }
    img{
      width: 4.8rem;
      margin: 0 auto;
      margin-top: 0.54rem;
    }
    .footer{
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      font-size: 0.42rem;
      color: #04d9d5;
      height: 1.2rem;
      line-height: 1.2rem;
      border-top:1px solid #e5e5e5;
    }
  }
  
}

</style>
